package com.jsshse.service.sys.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.jsshse.bean.UserRoleInfo;
import com.jsshse.dao.sys.UserDaoI;
import com.jsshse.service.base.impl.BaseServiceImpl;
import com.jsshse.service.sys.UserServiceI;
import com.jsshse.util.MD5;
import com.jsshse.util.Pages;
import com.jsshse.util.CommUtil;

import org.apache.log4j.Logger;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserServiceI {
	private static Logger log = Logger.getLogger(UserServiceImpl.class);
	@Resource
	private UserDaoI userDao;
	
	public Pages getAllUsers(Pages pages) {
		
		pages.setTotal(userDao.queryForListCnt(pages.getQueryCondition()));
		List list=userDao.queryForListByPage(pages.getAllQueryCond(), pages.getPageStart(), pages.getPageSize());
		pages.setDataList(list);
		return pages;
	}
	
	public boolean deleteUser(String id) {
		return userDao.delete(id)&&userDao.deleteUserRole(id);
	}

	public Map queryUserByUid(String id) {
		return userDao.queryById(id);
	}
	

	public boolean addUser(Map map) {
		map.put("user_id", CommUtil.getId());
		MD5 md5=new MD5();
		map.put("password",md5.getMD5ofStr(map.get("password").toString()) ); 
		userDao.add(map);
		
//		map.clear();
		map.put("ur_id", CommUtil.getId());
		map.put("dept_id", "1");
		return userDao.addUserRole(map);
		
	}


	public boolean updateUser(Map map) {
		 MD5 md5=new MD5();
		log.debug(queryUserByUid(map.get("user_id").toString()).get("password").toString());
		if(map.get("password").toString().length()>0)
			map.put("password",md5.getMD5ofStr(map.get("password").toString()) );
		else 
			map.put("password",userDao.queryById(map.get("user_id").toString()).get("password").toString());
		return userDao.update(map)&&userDao.updateUserRole(map);

	}
	
	public boolean updateUserInfo(Map map) {
		MD5 md5 = new MD5();
		if (map.get("password").toString().length() > 0)
			map.put("password", md5.getMD5ofStr(map.get("password").toString()));
		else
			map.put("password",userDao.queryById(map.get("user_id").toString()).get("password").toString());
		return userDao.updateUserInfo(map);
	}
	
	public int queryUserRoleCnt(Pages pages){
		return userDao.queryUserRoleCnt(pages.getQueryCondition());
	}
	
	public UserRoleInfo queryUserRoleInfo(String  paramCls) {
		return userDao.queryUserRoleInfo(paramCls);
	}
	
	public int queryUserCnt(String paramCls){
		return userDao.queryForListCnt(paramCls);
	}

	
}
